/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proxy;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

/**
 *
 * @author Hue
 */
public class AutoView implements Runnable {
    private String serverIp;
    private Properties config;
    
    public AutoView(Object obj, String serverIp) throws IOException {
        config = (Properties) obj;
        modifyHost(serverIp);
        this.serverIp = serverIp;
    }

    @Override
    public void run() {
        Robot robot;
        try {
            robot = new Robot();
        }
        catch (AWTException ex) {
            System.out.println("AutoView.run(): " + ex);
            return;
        }
        for(int i=0; i<6; i++) {
            if(i == 0) {
                cleanUp(robot);
            }
            viewAdv(robot);
        }
        changeVPN();
    }
    
    private void changeVPN() {
        try {
            URL url = new URL("http://" + serverIp + "/ChangeVPN.jsp");
            InputStream in = url.openStream();
            in.read();
            in.close();
        }
        catch(Exception ex) {
            System.out.println("changeVPN: " + ex);
        }
    }

    private void viewAdv(Robot robot) {
        leftClick(robot, "ff_bookmark");
        sleep(4000);
        leftClick(robot, "ff_download");
        mouseMove(robot, "ff_skip_ad");
        sleep(15000);
        leftClick(robot, "ff_skip_ad");
        sleep(500);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        sleep(4000);
        leftClick(robot, "ff_bookmark");
    }

    private void cleanUp(Robot robot) {
        rightClick(robot, "taskbar_point");
        sleep(500);
        //
        String pixelColor = getPixelColor(robot);
        String colorValue = config.getProperty("color_value");
        if(pixelColor == null || pixelColor.equals(colorValue)) {
            robot.keyPress(KeyEvent.VK_ESCAPE);
            robot.keyRelease(KeyEvent.VK_ESCAPE);
            sleep(500);
        }
        else {
            leftClick(robot, "close_ff_windows");
            sleep(4000);
        }
        //open Firefox [
        leftClick(robot, "taskbar_point");
        sleep(3000);
        //]
    }
    
    private String getPixelColor(Robot robot) {
        int[] p = parsePoint("color_at");
        if(p == null) {
            return null;
        }
        java.awt.Color c = robot.getPixelColor(p[0], p[1]);
        StringBuilder sb = new StringBuilder();
        String s = Integer.toHexString(c.getRed());
        if(s.length() < 2) {
            sb.append("0");
        }
        sb.append(s);
        s = Integer.toHexString(c.getGreen());
        if(s.length() < 2) {
            sb.append("0");
        }
        sb.append(s);
        s = Integer.toHexString(c.getBlue());
        if(s.length() < 2) {
            sb.append("0");
        }
        sb.append(s);
        return sb.toString().toUpperCase();
    }

    private int[] parsePoint(String key) {
        String s = config.getProperty(key);
        if(s == null || s.isEmpty()) {
            return null;
        }
        try {
            String[] arr = s.split(":");
            return new int[] {Integer.parseInt(arr[0]), Integer.parseInt(arr[1])};
        }
        catch(Exception ex) {
            System.out.println(ex + ": " + s);
        }
        return null;
    }
    
    public void leftClick(Robot robot, String key) {
        int[] p = parsePoint(key);
        if(p != null) {
            leftClick(robot, p[0], p[1]);
        }
    }
    
    public void doubleClick(Robot robot, String key) {
        int[] p = parsePoint(key);
        if(p != null) {
            doubleClick(robot, p[0], p[1]);
        }
    }
    
    public void rightClick(Robot robot, String key) {
        int[] p = parsePoint(key);
        if(p != null) {
            rightClick(robot, p[0], p[1]);
        }
    }

    public void leftClick(Robot robot, int x, int y) {
        mouseClick(robot, x, y, 1, false);
    }

    public void rightClick(Robot robot, int x, int y) {
        mouseClick(robot, x, y, 1, true);
    }

    public void doubleClick(Robot robot, int x, int y) {
        mouseClick(robot, x, y, 2, false);
    }

    private void mouseClick(Robot robot, int x, int y, int click, boolean isRightClick) {
        try {
            robot.mouseMove(x, y);
            int button = InputEvent.BUTTON1_MASK;
            if(isRightClick) {
                button = InputEvent.BUTTON3_MASK;
            }
            robot.mousePress(button);
            robot.mouseRelease(button);
            if(click == 2) {
                robot.mousePress(button);
                robot.mouseRelease(button);
            }
            Thread.sleep(1000);
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
    
    private void mouseMove(Robot robot, String key) {
        int[] p = parsePoint(key);
        if(p != null) {
            robot.mouseMove(p[0], p[1]);
        }
    }
    
    public void sleep(long millis) {
        try {
            Thread.sleep(millis);
        }
        catch(Exception ex) {
            System.out.println(ex);
        }
    }
    
    private void modifyHost(String ip) throws IOException {
        FileOutputStream out = new FileOutputStream("C:/WINDOWS/system32/drivers/etc/hosts");
        String str = ip + " adf.ly\r\n" + 
                     "127.0.0.1 www.youtube.com\r\n";
        out.write(str.getBytes());
        out.close();
    }
}
